package k.daniel.android.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daniel.danielalib.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ViewContainer<T extends ViewContainer.ContainerData> extends ViewGroup {
    private List<T> tags;
    Map<T, View> dataViewMap;

    private int sizeWidth;
    private int sizeHeight;
    int itemLayout;

    public int getItemLayout() {
        return itemLayout;
    }

    public void setItemLayout(int itemLayout) {
        this.itemLayout = itemLayout;
    }

    public ViewContainer(Context context) {
        super(context);
    }

    public ViewContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        mInflater = LayoutInflater.from(context);
    }

    public ViewContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    private LayoutInflater mInflater;

    public void setTags(List<T> tagList) {
        this.tags = tagList;
        this.removeAllViews();
        dataViewMap = new HashMap<>();
        if (tags != null && tags.size() > 0) {
            for (int i = 0; i < tags.size(); i++) {
                T containerData = tags.get(i);
                View view = mInflater.inflate(itemLayout, null);
                dataViewMap.put(containerData, view);
                processItemView(view, containerData);
                TextView tagView = (TextView) view.findViewById(R.id.tv_tag);
                tagView.setText(containerData.getTitle());
                view.setTag(R.id.tag_view_data, containerData);
                view.setTag(tagView);
                addView(view);
            }
        }
        postInvalidate();
    }

    public Map<T, View> getDataViewMap() {
        return dataViewMap;
    }

    public void setDataViewMap(Map<T, View> dataViewMap) {
        this.dataViewMap = dataViewMap;
    }

    public void processItemView(View view, ContainerData containerData) {
        if (onItemProcessedListener != null) {
            onItemProcessedListener.onItemProcessed(view, containerData);
        }
    }

    OnItemProcessedListener onItemProcessedListener;

    public OnItemProcessedListener getOnItemProcessedListener() {
        return onItemProcessedListener;
    }

    public void setOnItemProcessedListener(OnItemProcessedListener onItemProcessedListener) {
        this.onItemProcessedListener = onItemProcessedListener;
    }

    public interface OnItemProcessedListener {
        public void onItemProcessed(View view, ContainerData containerData);
    }

    /**
     * 计算 ChildView 宽高
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 计算 ViewGroup 上级容器为其推荐的宽高
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        MeasureHelper.logMeasureInfo(this, widthMeasureSpec, heightMeasureSpec);
        //计算 childView 宽高
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        int totalWidth = 0;
        int totalHeight = 0;
        totalHeight = getMultiTotalHeight(totalWidth, totalHeight);
        /**
         * 高度根据设置改变
         * 如果为 MATCH_PARENT 则充满父窗体，否则根据内容自定义高度
         */
        setMeasuredDimension(
                sizeWidth,
                (heightMode == MeasureSpec.EXACTLY ? sizeHeight : totalHeight));
    }

    int margin = 5;

    /**
     * 为 multiLine 模式布局，并计算视图高度
     *
     * @param totalWidth
     * @param totalHeight
     * @return
     */
    private int getMultiTotalHeight(int totalWidth, int totalHeight) {
        int childWidth;
        int childHeight;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            childWidth = child.getMeasuredWidth();
            childHeight = child.getMeasuredHeight();
            totalWidth += childWidth;
            if (i == 0) {
                totalHeight = childHeight;//初始化
            }
            // + marginLeft 保证最右侧与 ViewGroup 右边距有边界
            if (totalWidth > sizeWidth) {
                totalWidth = 0;
                totalHeight += childHeight;
                child.layout(
                        totalWidth,
                        totalHeight - childHeight,
                        totalWidth + childWidth,
                        totalHeight);
                totalWidth += childWidth;
            } else {
                child.layout(
                        totalWidth - childWidth,
                        totalHeight - childHeight,
                        totalWidth,
                        totalHeight);
            }
        }
        return totalHeight;
    }

    public interface ContainerData {
        public String getTitle();
    }

    public View getViewByData(ContainerData cd) {
        return dataViewMap.get(cd);
    }
}
